/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hermit.forum.tables;

import java.sql.Connection;
import java.sql.Types;
import java.util.LinkedHashMap;
import java.util.List;
import hermit.forum.db.Table;
import hermit.forum.db.TableObject;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Redin Pavel (hermit)
 */
public class Topic  extends Table {

    public int id=0;
    public String topic="";
    public String topic_ex="";
    public long date=System.currentTimeMillis();
    public long last_update=System.currentTimeMillis();
    public String author_id="";
    public int forum_id=0;
    public int state=0;

    public Topic(){
        init();
    }


    public Topic(int _id, String _topic, String _topic_ex, Long _date, Long _last_update, String _author_id, int _forum_id, int _state){
            id=_id;
            topic=_topic;
            topic_ex=_topic_ex;
            date=_date;
            last_update=_last_update;
            author_id=_author_id;
            forum_id=_forum_id;
            state=_state;
            init();
    }

    public void init(){
        name="topics";
        structure = new LinkedHashMap();
        structure.put("id", new TableObject(Types.INTEGER,id));
        structure.put("topic", new TableObject(Types.VARCHAR,topic));
        structure.put("topic_ex", new TableObject(Types.VARCHAR,topic_ex));
        structure.put("date", new TableObject(Types.TIMESTAMP,date));
        structure.put("last_update", new TableObject(Types.TIMESTAMP,last_update));
        structure.put("author_id", new TableObject(Types.VARCHAR,author_id));
        structure.put("forum_id", new TableObject(Types.INTEGER,forum_id));
        structure.put("state", new TableObject(Types.INTEGER,state));
    }

    @Override
    public String getTableName(){
       return this.name;
    }

    @Override
    public LinkedHashMap<String, TableObject> getStructure() {
        return structure;
    }

    @Override
    public Table getTableObject(Connection db, String q){
        Topic t = new Topic();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
            stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rSet = stmt.executeQuery(q);
            rSet.next();
            t.id = rSet.getInt(1);
            t.topic = rSet.getString(2);
            t.topic_ex = rSet.getString(3);
            t.date = rSet.getTimestamp(4).getTime();
            t.last_update = rSet.getTimestamp(5).getTime();
            t.author_id = rSet.getString(6);
            t.forum_id = rSet.getInt(7);
            t.state = rSet.getInt(8);
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        return t;
    }


    @Override
    public ArrayList<Table> getTableList(Connection db, String q){
        ArrayList<Table> list = new ArrayList<Table>();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
        	stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        	rSet = stmt.executeQuery(q);
            while(rSet.next()) {
            Topic t = new Topic();
            t.id = rSet.getInt(1);
            t.topic = rSet.getString(2);
            t.topic_ex = rSet.getString(3);
            t.date = rSet.getTimestamp(4).getTime();
            t.last_update = rSet.getTimestamp(5).getTime();
            t.author_id = rSet.getString(6);
            t.forum_id = rSet.getInt(7);
            t.state = rSet.getInt(8);
                list.add(t);
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        list.trimToSize();
        return list;
    }

  

}
